package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.modular.system.entity.ExamPayLog;
import cn.stylefeng.guns.modular.system.mapper.ExamPayLogMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 考试报名支付记录 服务类
 * </p>
 *
 * @author zfy
 * @since 2020-08-13
 */
@Service
public class ExamPayLogService extends ServiceImpl<ExamPayLogMapper,ExamPayLog> {

    public void add(String examIds, String userIds, String out_trade_no, String transaction_id, Double total_fee, String createUser) {
        if(ToolUtil.isEmpty(out_trade_no) || ToolUtil.isEmpty(transaction_id) || isExamPayLog(out_trade_no,transaction_id) ){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        ExamPayLog examPayLog = new ExamPayLog();
        examPayLog.setOutTradeNo(out_trade_no);
        examPayLog.setTransactionId(transaction_id);
        examPayLog.setExamIds(examIds);
        examPayLog.setUserIds(userIds);
        examPayLog.setTotalFee(total_fee);
        examPayLog.setCreateUser(Long.parseLong(createUser));
        examPayLog.setCreateTime(LocalDateTime.now());
        this.save(examPayLog);
    }

    private Boolean isExamPayLog (String out_trade_no,String transaction_id){
        ExamPayLog examPayLog = this.getOne(new QueryWrapper<ExamPayLog>()
                .eq("out_trade_no", out_trade_no)
                .eq("transaction_id",transaction_id).eq("effective", 1));
        return examPayLog == null ? false:true;
    }
}
